package org.xqp.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xqp.basic.exception.MyException;
import org.xqp.basic.util.JsonResult;
import org.xqp.user.dto.LoginDto;
import org.xqp.user.service.ILoginService;


import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @PostMapping("account")
    public JsonResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map = loginService.account(loginDto);
            return JsonResult.me().setResultObj(map);  //
        }catch (MyException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }
    /**
     * wechat 微信登录
     */
    @PostMapping("/wechat")
    public JsonResult wechat(@RequestBody Map<String,String> params){
        try {
            JsonResult wechat = loginService.wechat(params);
            return wechat;
        } catch (MyException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }

    /**
     * wechat 微信绑定
     */
    @PostMapping("/binder/wechat")
    public JsonResult binder(@RequestBody Map<String,String> params){
        try {
            Map<String,Object> map= loginService.binder(params);
            return JsonResult.me().setResultObj(map);
        } catch (MyException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统异常");
        }
    }

}

